#include "menu.h"
#include "gui_al.h"
#include "callbacks.h"
#include "tabs.h"
#include "gui_io.h"
#include "about.h"

GtkWidget *elog_mnu_initialize(GtkAccelGroup *accel_group)
{
	printf("Making the menu...\n");
	GtkWidget *cut1;
	GtkWidget *copy1;
	GtkWidget *paste1;
	GtkWidget *delete1;
	GtkWidget *preferences;
	GtkWidget *save1;
	GtkWidget *separatormenuitem1;
	GtkWidget *quit1;
	GtkWidget *menubar1;
	GtkWidget *menuitem4;
	GtkWidget *menuitem4_menu;
	GtkWidget *menuitem5;
	GtkWidget *menuitem5_menu;
	GtkWidget *menuitem6;
	GtkWidget *menuitem7;
	GtkWidget *menuitem7_menu;
	GtkWidget *about1;
	GtkWidget *help;

	menubar1 = gtk_menu_bar_new();
	GtkWidget *hbox7 = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox7), menubar1, TRUE, TRUE, 0);


	menuitem4 = gtk_menu_item_new_with_mnemonic(_("_File"));
	gtk_container_add(GTK_CONTAINER(menubar1), menuitem4);

	menuitem4_menu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem4),
				  menuitem4_menu);

	save1 =
	    gtk_image_menu_item_new_from_stock("gtk-save", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem4_menu), save1);

	separatormenuitem1 = gtk_separator_menu_item_new();
	gtk_container_add(GTK_CONTAINER(menuitem4_menu),
			  separatormenuitem1);
	gtk_widget_set_sensitive(separatormenuitem1, FALSE);

	quit1 =
	    gtk_image_menu_item_new_from_stock("gtk-quit", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem4_menu), quit1);

	menuitem5 = gtk_menu_item_new_with_mnemonic(_("_Edit"));
	gtk_container_add(GTK_CONTAINER(menubar1), menuitem5);

	menuitem5_menu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem5),
				  menuitem5_menu);

	GtkWidget *undo =
	    gtk_image_menu_item_new_from_stock("gtk-undo", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), undo);

	GtkWidget *redo =
	    gtk_image_menu_item_new_from_stock("gtk-redo", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), redo);



	cut1 = gtk_image_menu_item_new_from_stock("gtk-cut", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), cut1);

	copy1 =
	    gtk_image_menu_item_new_from_stock("gtk-copy", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), copy1);

	paste1 =
	    gtk_image_menu_item_new_from_stock("gtk-paste", accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), paste1);

	delete1 =
	    gtk_image_menu_item_new_from_stock("gtk-delete", accel_group);

	preferences =
	    gtk_image_menu_item_new_from_stock("gtk-preferences",
					       accel_group);
	gtk_container_add(GTK_CONTAINER(menuitem5_menu), preferences);

	menuitem6 = gtk_menu_item_new_with_mnemonic(_("_View"));
	gtk_container_add(GTK_CONTAINER(menubar1), menuitem6);

	_viewMenu = menuitem6;

	menuitem7 = gtk_menu_item_new_with_mnemonic(_("_Help"));
	gtk_container_add(GTK_CONTAINER(menubar1), menuitem7);

	menuitem7_menu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem7),
				  menuitem7_menu);

	help = gtk_menu_item_new_with_mnemonic(_("_Manual"));
	gtk_container_add(GTK_CONTAINER(menuitem7_menu), help);

	about1 = gtk_menu_item_new_with_mnemonic(_("_About"));
	gtk_container_add(GTK_CONTAINER(menuitem7_menu), about1);



	GtkWidget *mnu_srch_btn = gtk_menu_item_new();
	gtk_box_pack_end(GTK_BOX(hbox7), mnu_srch_btn, FALSE, FALSE, 0);



	GtkWidget *image1 =
	    gtk_image_new_from_stock("gtk-find", GTK_ICON_SIZE_MENU);
	gtk_widget_show(image1);
	gtk_container_add(GTK_CONTAINER(mnu_srch_btn), image1);

	_menu_srch_entry = gtk_entry_new();


	gtk_box_pack_end(GTK_BOX(hbox7), _menu_srch_entry, FALSE, FALSE,
			 0);
	gtk_entry_set_text(GTK_ENTRY(_menu_srch_entry), "");


	g_signal_connect((gpointer) save1, "activate",
			 G_CALLBACK(on_save1_activate), NULL);
	g_signal_connect((gpointer) quit1, "activate",
			 G_CALLBACK(on_quit1_activate), NULL);

	g_signal_connect((gpointer) undo, "activate",
			 G_CALLBACK(on_undo_activate), NULL);

	g_signal_connect((gpointer) redo, "activate",
			 G_CALLBACK(on_redo_activate), NULL);

	g_signal_connect((gpointer) cut1, "activate",
			 G_CALLBACK(on_cut1_activate), NULL);
	g_signal_connect((gpointer) copy1, "activate",
			 G_CALLBACK(on_copy1_activate), NULL);
	g_signal_connect((gpointer) paste1, "activate",
			 G_CALLBACK(on_paste1_activate), NULL);
	g_signal_connect((gpointer) delete1, "activate",
			 G_CALLBACK(on_delete1_activate), NULL);
	g_signal_connect((gpointer) preferences, "activate",
			 G_CALLBACK(on_preferences_activate), NULL);
	g_signal_connect((gpointer) about1, "activate",
			 G_CALLBACK(on_about1_activate), NULL);
	g_signal_connect((gpointer) help, "activate",
			 G_CALLBACK(on_help_activate), NULL);

	g_signal_connect_after((gpointer) _menu_srch_entry,
			       "key-release-event",
			       G_CALLBACK(on_srch_bar_key_release), NULL);
	g_signal_connect_after((gpointer) _menu_srch_entry,
			       "insert-at-cursor",
			       G_CALLBACK(on_srch_bar_btn_release), NULL);
	g_signal_connect_after((gpointer) _menu_srch_entry,
			       "paste-clipboard",
			       G_CALLBACK(on_srch_bar_btn_release), NULL);
	g_signal_connect_after((gpointer) _menu_srch_entry,
			       "cut-clipboard",
			       G_CALLBACK(on_srch_bar_btn_release), NULL);



	_mainMenuBar = menubar1;
	return hbox7;
}




/*****Callbacks:******/
void on_save1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	printf("saving\n");
	elog_gui_io_save();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}

void on_undo_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_undo();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}

void on_redo_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_redo();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}


void on_quit1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	gtk_main_quit();
	gtk_main_quit();
	gtk_main_quit();	//is 6 enough?! Gee I hope so ;).
	gtk_main_quit();
	gtk_main_quit();
	gtk_main_quit();
}


void on_cut1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_cut();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}


void on_copy1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_copy();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}


void on_paste1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCK_CALLBACK);
	elog_tab_act_paste();
	elog_gui_al_setLockMode(ELOG_GUI_AL_LOCKED);
}


void on_delete1_activate(GtkMenuItem * menuitem, gpointer user_data)
{

}


void on_about1_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	create_about();
}

void on_help_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	elog_gui_io_open_manual();
}
